package com.example.sxmall.service.action.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.example.sxmall.auth.Authentication;
import com.example.sxmall.auth.UserInfo;
import com.example.sxmall.controller.shopCart.ValidStatusConstant;
import com.example.sxmall.controller.shopCart.vo.ShopCartVO;
import com.example.sxmall.controller.shopCart.vo.StoreProductVO;
import com.example.sxmall.redis.ShopCartKey;
import com.example.sxmall.service.action.ActionCartService;
import com.example.sxmall.service.dto.OperateCartDTO;
import com.example.sxmall.service.dto.ShopCartDTO;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author whh-yt
 * @Date 2022/3/17 4:28 下午
 */
@Service
public class ClearCartActionService extends ActionCartService {

    @Override
    public ShopCartVO execute(OperateCartDTO operateCartDTO) {
        UserInfo userInfo = Authentication.getUserInfo();
        ShopCartDTO shopCartCache = redisService.get(ShopCartKey.SHOP_CART_KEY, userInfo.getUnionId(), ShopCartDTO.class);
        for (StoreProductVO storeProductVO : shopCartCache.getStoreProductList()) {
            storeProductVO.setProductList(storeProductVO.getProductList()
                    .stream().filter(item -> item.getOnShelf().equals(ValidStatusConstant.YES_INT)).collect(Collectors.toList()));
        }
        shopCartCache.getStoreProductList().removeIf(item -> CollectionUtils.isEmpty(item.getProductList()));
        redisService.set(ShopCartKey.SHOP_CART_KEY, userInfo.getUnionId(), shopCartCache);
        return super.execute(operateCartDTO);
    }
}
